package org.casper.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.casper.bean.User;
import org.casper.util.DBUtil;

/**
 * 操作t_user表的类
 * project_name:testweb<br>
 * package_name:org.casper.dao<br>
 * file_name:UserDAO.java<br>
 * @author Casper<br>
 * create_date:2015年10月12日 下午2:26:09<br>
 * modified_date:
 * description:
 *
 */
public class UserDAO {
	
	/**
	 * 查询所有的用户
	 * description:
	 * 2015年10月12日 下午2:27:36
	 * casper
	 * ArrayList<User>
	 * @return
	 */
	public ArrayList<User> selectAllUsers(){
		ArrayList<User> allUsers = new ArrayList<User>();
		Connection conn= null;
		PreparedStatement pstmt= null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement("select id,username,userpass from t_user");
			rs = pstmt.executeQuery();
			User user = null;
			while(rs.next()){
				user = new User(rs.getInt("id"),rs.getString("username"), rs.getString("userpass"));
				allUsers.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeAll(conn, pstmt, rs);
		}
		return allUsers;
	}

}
